const CommentModle = require("./models/commentModel");
const BlogModel = require("./models/Blog");

/**
 * 添加一条评论数据
 * @param {object} comment 
 * @returns 
 */
exports.createCommentDao = async (comment) => {
    return await CommentModle.create(comment);
}

/**
 * 删除一条评论
 * @param {number} id 
 * @returns 
 */
exports.deleteCommentDao = async (id) => {
    return await CommentModle.destroy({
        where: {
            id
        }
    })
}

/**
 * 分页获取评论数据
 * @param {object} param0 
 * @returns 
 */
exports.getCommentPagingDao = async (offset, limit, where) => {
    return await CommentModle.findAndCountAll({
        offset,
        limit,
        where,
        include: {
            model: BlogModel,
            as: "blog",
            attributes: ["id", "title"]
        },
        attributes: {
            exclude: ["blogId"]
        }
    })
}

exports.getBlogCommentCountDao = async (where) => {
    return await CommentModle.count({
        where
    })
}

/**
 * 根据文章id删除评论
 * @param {number} blogId 
 * @returns 
 */
exports.deleteCommentByBlogId = async (blogId) => {
    console.log(blogId);
    return await CommentModle.destroy({
        where: {
            blogId
        }
    })
}